<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="950px" :clickModalClose="true"
			@confirm="handleSubmit" @close="handleClose" :footershow="false">
			<mtable :data="formData.accessories[10]" :type="`accessories`" :list="options.tableTile" :operation="false"
				:tableheight="tableheight" @handleAdds="handleAdds(1)" :bill="false">
				<template #tool="{item}">
					<el-table-column v-if="item.display==1" sortable :label="item.name" show-overflow-tooltip
						:prop="item.value" :width="item.width">
						<template #default="{row}">
							<div v-if="item.name=='存储数量'">
								{{row.quantity}}{{row.unit_name}}
							</div>
						</template>
					</el-table-column>
				</template>
			</mtable>
			<div style="padding: 10px 0;font-size: 16px;color: #333;">
				设备配件报废情况
			</div>
			<mtable :data="formData.accessories[30]" :type="`scrapping_accessories`" :list="options.tableTiles" :operation="false"
				:tableheight="tableheight" @handleAdds="handleAdds(2)" :bill="false">
				<template #tool="{item}">
					<el-table-column v-if="item.display==1" sortable :label="item.name" show-overflow-tooltip
						:prop="item.value" :width="item.width">
						<template #default="{row}">
							<div v-if="item.name=='作废数量'">
								{{row.quantity}}{{row.unit_name}}
							</div>
						</template>
					</el-table-column>
				</template>
			</mtable>
		</popup>
		<EdittablePopup v-if="showTable" ref="editRefs" :type="typeTable" @success="getOptions"
			@close="showTable=false" />
	</div>
</template>
<script lang="ts" setup>
	import type {
		FormInstance
	} from 'element-plus'
	import Popup from '@/components/popup/index.vue'
	import EdittablePopup from '@/components/settable/index.vue'
	import {
		adminAdd,
		adminEdit
	} from '@/api/unit/list'

	import {
		getCommonFieldDisplay
	} from '@/api/tools/code'
	import mtable from '@/components/table/index.vue'
	const emit = defineEmits(['success', 'close'])
	const formRef = shallowRef < FormInstance > ()
	const typeTable = ref('accessories')//scrapping_accessories
	const editRefs = shallowRef < InstanceType < typeof EdittablePopup >> ()
	const popupRef = shallowRef < InstanceType < typeof Popup >> ()
	const mode = ref('add')
	const tableheight = ref('200')
	const popupTitle = computed(() => {
		return mode.value == 'edit' ? '设备配件库存详情' : '设备配件库存详情'
	})
	const formData = reactive({
		accessories: []
	})
	const showTable = ref(false)
	const handleAdds = async (val:any) => {
		
		if(val==1){
			typeTable.value = 'accessories'
		}else{
			typeTable.value = 'scrapping_accessories'
		}
		showTable.value = true
		await nextTick()
		editRefs.value?.open('add')
	}

	const options = reactive({
		tableTile: [],
		tableTiles:[]
	})

  // 李哥修改
    const getOptions = async() => {
   var data1 = await 
  getCommonFieldDisplay({type: 'accessories'});
  options.tableTile = data1;
   var data = await 
  getCommonFieldDisplay({type: 'scrapping_accessories'});
  options.tableTiles = data;
 }
  //高扬修改
	// const getOptions = () => {
	// 	getCommonFieldDisplay({
	// 		type: 'accessories'
	// 	}).then((data: any) => {
	// 		options.tableTile = data
	// 		getCommonFieldDisplay({
	// 			type: 'scrapping_accessories'
	// 		}).then((data: any) => {
	// 			options.tableTiles = data
	// 		})
	// 	})
	// }

	const handleSubmit = async () => {
		await formRef.value?.validate()
		mode.value == 'edit' ? await adminEdit(formData) : await adminAdd(formData)
		popupRef.value?.close()
		emit('success')
	}

	const open = (type = 'add') => {
		mode.value = type
		popupRef.value?.open()
	}

	const setFormData = (data: Record < any, any > ) => {
		for (const key in formData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				formData[key] = data[key]
			}
		}

	}

	const handleClose = () => {
		emit('close')
	}
	getOptions()
	defineExpose({
		open,
		setFormData
	})
</script>
